package writtenexamination.laohu;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;

/**
 * @author cyt
 * @create 2020-10-25 下午 4:53
 */
public class Tb {
    public static void main(String[] args) {
        ListNode node5 = new ListNode(4, null);
        ListNode node4 = new ListNode(5, node5);
        ListNode node3 = new ListNode(1, node4);
        ListNode node2 = new ListNode(3, node3);
        ListNode node1 = new ListNode(2, node2);
        ListNode listNode = new Tb().lineUp(node1);
        while (listNode != null) {
            System.out.print(listNode.val + "->");
            listNode = listNode.next;
        }
    }

    public ListNode lineUp(ListNode head) {
        // write code here
        ArrayList<Integer> list = new ArrayList<>();
        while (head != null) {
            list.add(head.val);
            head = head.next;
        }
        ArrayList<Integer> integers = new ArrayList<>(list);
        list.clear();
        int k = 0;
        for (int i = 0; i < integers.size(); i++) {
            if ((i + 1) % 2 != 0) {
                list.add(k++, integers.get(i));
            }
        }
        for (int i = 0; i < integers.size(); i++) {
            if ((i + 1) % 2 == 0) {
                list.add(k++, integers.get(i));
            }
        }
        ListNode nodes = head;
        ListNode newHead = nodes;
        ListNode temp = null;
        for (int i = 0; i < list.size(); i++) {
            nodes.val = list.get(i);
            temp = new ListNode(list.get(i));
            nodes.next = temp;
            nodes = nodes.next;
        }
        return newHead;
    }
}


class ListNode {
    int val;
    ListNode next;

    public ListNode(int i, ListNode o) {
        this.val = i;
        this.next = o;
    }
    public ListNode(Integer integer) {
        this.val = integer;

    }
}
